package cn.com.headfree.sysmenu.dao.impl;

import cn.com.headfree.sysmenu.dao.IUserRoleDao;
import cn.com.headfree.sysmenu.entity.TSysUserRole;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("userRoleDao")
public class UserRoleDaoImpl extends BaseDaoImpl<TSysUserRole> implements IUserRoleDao {
    private static final Logger LOG = Logger.getLogger(UserRoleDaoImpl.class);

    //通过角色ID查询角色使用数量
    @Override
    public List<TSysUserRole> getUserRoleListByRoleId(String roleId){
        String sql = "  select ur.id,ur.role_id,ur.erp_no " +
                " from t_sys_user_role ur " +
                " where 1=1 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);

        ArrayList<Object> arrayList = new ArrayList<Object>();
        //角色id
        if (!StringUtil.isNullOrEmpty(roleId)) {
            sqlBuilder.append(" AND ur.role_id = ? ");
            arrayList.add(roleId);
        }
        return this.findAllBySQL(TSysUserRole.class, sqlBuilder.toString(), null,false, arrayList.toArray());
    }

    //通过用户ID查询用户角色列表
    @Override
    public List<TSysUserRole> getUserRoleListByUserId(String userId){
        String sql = "  select ur.id,ur.role_id,ur.erp_no " +
                " from t_sys_user_role ur " +
                " where 1=1 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);

        ArrayList<Object> arrayList = new ArrayList<Object>();
        //用户id
        if (!StringUtil.isNullOrEmpty(userId)) {
            sqlBuilder.append(" AND ur.erp_no = ? ");
            arrayList.add(userId);
        }
        return this.findAllBySQL(TSysUserRole.class, sqlBuilder.toString(), null,false, arrayList.toArray());
    }

    //通过用户ID删除用户和角色关联
    @Override
    public void deleteUserRoleByUserId(String userId){
        String sql = "delete from t_sys_user_role ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" where 1=1 ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and erp_no = #{erp_no,jdbcType=VARCHAR} ");
        params.put("erp_no", userId);
        this.executeSql(sqlBuilder.toString(), params);
    }

    //批量删除用户和角色关联
    @Override
    public void deleteUserRole(String[] userIds){
        String sql = "delete from t_sys_user_role ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" where 1=1 ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and erp_no in ('").append(String.join("','",userIds)).append("') ");
        this.executeSql(sqlBuilder.toString(), params);
    }

    //批量取消授权用户
    @Override
    public void deleteAuthUsers(String roleId,String userId) {
        String sql = "delete from t_sys_user_role where role_id=#{role_id,jdbcType=VARCHAR} and erp_no =#{erp_no,jdbcType=VARCHAR} ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("role_id",roleId);
        params.put("erp_no",userId);
        this.executeSql(sqlBuilder.toString(), params);
    }

    //批量授权用户
    @Override
    public void saveUserRole(String roleId,String userId) {
        String sql = "insert into t_sys_user_role(id, erp_no, role_id) values  (#{id,jdbcType=VARCHAR},#{erp_no,jdbcType=VARCHAR},#{role_id,jdbcType=VARCHAR}) ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", IdGen.uuid());
        params.put("role_id",roleId);
        params.put("erp_no",userId);
        this.executeSql(sqlBuilder.toString(), params);
    }

}
